package com.yanqu.road.dao.impl.activity.specialcookboy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.specialcookboy.SpecialCookBoyUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SpecialCookBoyUserDataDaoImpl extends TempDataObjectDao<SpecialCookBoyUserData> {


    public static String getSpecialCookBoyUserDataMapSql = "select * from `t_u_special_cook_boy_user_data`";
    public Map<Long, SpecialCookBoyUserData> getSpecialCookBoyUserDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SpecialCookBoyUserData> userDataMap= new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSpecialCookBoyUserDataMapSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                SpecialCookBoyUserData parse = parse(rs);
                userDataMap.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return userDataMap;
    }

    public static String getSpecialCookBoyUserDataSql = "select * from `t_u_special_cook_boy_user_data` where `userId`=?";
    public SpecialCookBoyUserData getSpecialCookBoyUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSpecialCookBoyUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSpecialCookBoyUserDataSql = "update `t_u_special_cook_boy_user_data` set guideBit=?,passInfo=?,shareCode=?,shareCodeRefreshTime=?,usedOthersShareCode=?,lastResetUsedOthersShareCodeTime=?,shareCodeUsePlayers=?,unlockedTips=? where userId=?";
    @Override
    protected boolean updateData(SpecialCookBoyUserData specialCookBoyUserData) {
        specialCookBoyUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, specialCookBoyUserData.getGuideBit()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getPassInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, specialCookBoyUserData.getShareCode()));
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getShareCodeRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getUsedOthersShareCode())));
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getLastResetUsedOthersShareCodeTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getShareCodeUsePlayers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getUnlockedTips())));
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSpecialCookBoyUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSpecialCookBoyUserDataSql = "insert into `t_u_special_cook_boy_user_data`(userId,guideBit,passInfo,shareCode,shareCodeRefreshTime,usedOthersShareCode,lastResetUsedOthersShareCodeTime,shareCodeUsePlayers,unlockedTips) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SpecialCookBoyUserData specialCookBoyUserData) {
        specialCookBoyUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, specialCookBoyUserData.getGuideBit()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getPassInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, specialCookBoyUserData.getShareCode()));
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getShareCodeRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getUsedOthersShareCode())));
        para.put(i++, new DbParameter(Types.BIGINT, specialCookBoyUserData.getLastResetUsedOthersShareCodeTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getShareCodeUsePlayers())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(specialCookBoyUserData.getUnlockedTips())));
        if (getDb().executeNoneQuery(insertSpecialCookBoyUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SpecialCookBoyUserData parseData(ResultSet rs) throws SQLException {
        SpecialCookBoyUserData specialCookBoyUserData = new SpecialCookBoyUserData();
        specialCookBoyUserData.setUserId(rs.getLong("userId"));
        specialCookBoyUserData.setGuideBit(rs.getInt("guideBit"));
        specialCookBoyUserData.setPassInfo(JSON.parseObject(rs.getString("passInfo"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        specialCookBoyUserData.setShareCode(rs.getString("shareCode"));
        specialCookBoyUserData.setShareCodeRefreshTime(rs.getLong("shareCodeRefreshTime"));
        specialCookBoyUserData.setUsedOthersShareCode(JSON.parseObject(rs.getString("usedOthersShareCode"), new TypeReference<java.util.List<java.lang.String>>() {}));
        specialCookBoyUserData.setLastResetUsedOthersShareCodeTime(rs.getLong("lastResetUsedOthersShareCodeTime"));
        specialCookBoyUserData.setShareCodeUsePlayers(JSON.parseObject(rs.getString("shareCodeUsePlayers"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        specialCookBoyUserData.setUnlockedTips(JSON.parseObject(rs.getString("unlockedTips"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        if (specialCookBoyUserData.getPassInfo() == null){
            specialCookBoyUserData.setPassInfo(new ConcurrentHashMap<Integer, Integer>());
        }
        if (specialCookBoyUserData.getUsedOthersShareCode() == null){
            specialCookBoyUserData.setUsedOthersShareCode(new ArrayList<String>());
        }
        if (specialCookBoyUserData.getShareCodeUsePlayers() == null){
            specialCookBoyUserData.setShareCodeUsePlayers(new ArrayList<Long>());
        }
        if (specialCookBoyUserData.getUnlockedTips() == null){
            specialCookBoyUserData.setUnlockedTips(new ArrayList<Integer>());
        }
        return specialCookBoyUserData;
    }

}
